<?php

/**
 * @BelongProject: flami_mangers
 * @Author: Zwc
 * @CreateTime: 2025-10-19 15:18:41
 * @Description: TODO
 */

namespace app\admin\controller;

use app\admin\logic\AgreementLogic;
use app\admin\model\AgreementModel;
use app\admin\service\CountryService;
use cmf\controller\AdminBaseController;

class AgreementController extends AdminBaseController
{

    /**
     * 列表
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        $params = $this->request->param();
        $countryData = CountryService::getRecommendLanguage();
        $this->assign("countryData", $countryData);
        if (empty($params['langId'])) {
            $params['langId'] = $countryData[0]['id'];
        }
        $dataList = (new AgreementLogic())->getData($params);
        $dataList->appends($params);

        $page = $dataList->render();
        $this->assign("page", $page);
        $this->assign('list', $dataList);


        if (!empty($params['langId'])) {
            $this->assign('langId', $params['langId']);
        }else {
            $this->assign('langId', $countryData[0]['id']);
        }

        return $this->fetch();
    }

    /**
     * 添加
     * @return mixed|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        if (request()->isPost()) {
            $params = $this->request->post();
            if (empty($params['lang_id'])) {
                $this->error('请选择语言');
            }
            if (empty($params['title'])) {
                $this->error('请填写协议名称');
            }

            if (empty($params['type'])) {
                $this->error('请选择协议类型');
            }

            if (empty($params['content'])) {
                $this->error('协议内容不能为空');
            }
            $model = new AgreementModel();
            $model->insert($params);
            $this->success("添加成功！");
        }else {
            $this->assign("countryData", $this->recommendLanguageData);
            $this->assign("agreementTypeData", (new AgreementLogic())->getAgreementType() );
            return $this->fetch();
        }
    }

    /**
     * 修改
     * @return mixed|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit() {
        if ($this->request->isPost()) {
            $params = $this->request->post();
//            if (empty($params['lang_id'])) {
//                $this->error('请选择语言');
//            }
            if (empty($params['title'])) {
                $this->error('请填写协议名称');
            }

            if (empty($params['type'])) {
                $this->error('请选择协议类型');
            }

            if (empty($params['content'])) {
                $this->error('协议内容不能为空');
            }
            $model = new AgreementModel();
            $model->where(['id' => $params['id']])->data($params)->save();
            $this->success("操作成功！");
        }else {
            $params = $this->request->get();
            $model = new AgreementModel();
            $data = $model->where(['id' => $params['id']])->find();
            $this->assign('data', $data);
            $this->assign("agreementTypeData", (new AgreementLogic())->getAgreementType() );
            return $this->fetch();
        }
    }

    /**
     * 删除
     * @return void
     */
    public function del()
    {
        $model = new AgreementModel();
        $model->where(['id' => $this->request->get('id')])->data(['del_flag' => 1])->save();
        $this->success("操作成功");
    }

}